package main

import (
    "io/ioutil"
    "log"

    "gopkg.in/yaml.v2"
)

/*
结构字段只有在导出时才进行数据编出(首字母大写)，
并且使用小写的字段名作为默认键进行数据编出
*/
type repExportInfo struct {
    RepEnabled bool   `yaml:"repEnabled"`
    RepOldWord string `yaml:"repOldWord"`
    RepNewWord string `yaml:"repNewWord"`
}

type sftpInfo struct {
    SftpEnabled  bool   `yaml:"sftpEnabled"`
    UserName     string `yaml:"userName"`
    PassWord     string `yaml:"passWord"`
    HostAdress   string `yaml:"hostAdress"`
    HostPort     int64  `yaml:"hostPort"`
    RemoteFolder string `yaml:"remoteFolder"`
}

type conf struct {
    TarEnabled           bool          `yaml:"tarEnabled"`
    TarFileName          string        `yaml:"tarFileName"`
    CheckPath            string        `yaml:"checkPath"`
    ExportExcelEnabled   bool          `yaml:"exportExcelEnabled"`
    ExportExcelName      string        `yaml:"exportExcelName"`
    ExportExcelSheetName string        `yaml:"exportExcelSheetName"`
    ExportExcelColName   string        `yaml:"exportExcelColName"`
    RepExportInfo        repExportInfo `yaml:"repExportInfo"`
    SftpInfo             sftpInfo      `yaml:"sftpInfo"`
}

func getConf() *conf {
    // 配置文件位置
    yamlFilePath := getCurrentPath() + getPathDelimiter() + "config.yaml"
    log.Printf("开始读取配置文件: %s", yamlFilePath)
    if !Exists(yamlFilePath) || !IsFile(yamlFilePath) {
        log.Printf("配置文件不存在,请把配置文件放置当前工作目录: %s", yamlFilePath)
        return nil
    }
    var c *conf
    yamlFile, err := ioutil.ReadFile(yamlFilePath)
    if err != nil {
        log.Printf("读取配置文件失败，报错信息: %#v ", err)
        return c
    }
    err = yaml.Unmarshal(yamlFile, &c)
    if err != nil {
        log.Fatalf("解析yaml文件失败: %#v", err)
    }
    return c
}
